package com.ileaf.java8.gui;

import com.ileaf.java8.AesUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author Lianlb
 * @date 2019-11-24 21:43
 */
public class Decrypt extends JFrame implements Function {

    private JPanel panel;
    private JButton homeButton;
    private JTextField text;
    private JButton submit;
    private JLabel label;
    private JTextField result;
    private JPanel panelTop;
    private JPanel panelContent;
    private JPanel panelFooter;

    @Override
    public void createUIComponents() {
        // TODO: place custom component creation code here
        addComponentsAction();
    }
    Decrypt(String title) throws HeadlessException {
        super(title);
        createUIComponents();
        setContentPane(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(600, 400));
        setLocation(300, 100);
    }

    @Override
    public void addComponentsAction() {
        homeButton.addActionListener(event -> {
            // 销毁home页
            dispose();
            // 打开主窗口
            Home home = new Home("系统主页");
            home.pack();
            home.setVisible(true);
        });
        submit.addActionListener(event -> {
            decrypt();
        });
        text.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    decrypt();
                }
            }
        });
    }

    private void decrypt() {
        String txt = this.text.getText();
        if (!"".equals(txt)) {
            String hex = AesUtil.decryptHex(txt, AesUtil.MY_PASSWORD);
            result.setText(hex);
        }
    }

}
